package net.magicalcloud.magicalcloud.view;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;



import static android.view.MotionEvent.ACTION_CANCEL;
import static android.view.MotionEvent.ACTION_DOWN;
import static android.view.MotionEvent.ACTION_MOVE;
import static android.view.MotionEvent.ACTION_UP;

/**
 * 这个recycleview在没有数据的时候会有个空view 提示，并且上下滑可以显示隐藏底部导航栏
 * Created by hiwhitley on 2016/4/28.
 */
public class EmptyRecyclerView extends RecyclerView {

    private View emptyView;

    final private AdapterDataObserver observer = new AdapterDataObserver() {
        @Override
        public void onChanged() {
            checkIfEmpty();
        }

        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            checkIfEmpty();
        }

        @Override
        public void onItemRangeRemoved(int positionStart, int itemCount) {
            checkIfEmpty();
        }
    };

    public EmptyRecyclerView(Context context) {
        super(context);
    }

    public EmptyRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public EmptyRecyclerView(Context context, AttributeSet attrs,
                             int defStyle) {
        super(context, attrs, defStyle);
    }

    private void checkIfEmpty() {
        if (emptyView != null && getAdapter() != null) {
            final boolean emptyViewVisible =
                    getAdapter().getItemCount() == 0;
            emptyView.setVisibility(emptyViewVisible ? VISIBLE : GONE);
            setVisibility(emptyViewVisible ? GONE : VISIBLE);
        }
    }


//    private int mLastX;
//    private int mLastY;
//    @Override
//    public boolean dispatchTouchEvent(MotionEvent ev) {
//        int y=(int)ev.getY();
//       // int x=(int)ev.getX();
//        switch (ev.getAction()) {
//
//            case ACTION_DOWN:
//                break;
//
//            case ACTION_MOVE:
//                //int dX=x-mLastX;
//                int dY=y-mLastY;
//
//                //滑动距离太短，忽略，保持现状
//                if(Math.abs(dY)<10){
//                    break;
//                }
//                if(dY>0){           //下滑
//                    AppApplication.getBottomNavigation().restoreBottomNavigation();
//                }else if(dY<0){     //上滑
//                    AppApplication.getBottomNavigation().hideBottomNavigation();
//                }
//                break;
//        }
//
//       // mLastX=x;
//        mLastY=y;
//        return super.dispatchTouchEvent(ev);
//    }


    @Override
    public void setAdapter(Adapter adapter) {
        final Adapter oldAdapter = getAdapter();
        if (oldAdapter != null) {
            oldAdapter.unregisterAdapterDataObserver(observer);
        }
        super.setAdapter(adapter);
        if (adapter != null) {
            adapter.registerAdapterDataObserver(observer);
        }

        checkIfEmpty();
    }

    public void setEmptyView(View emptyView) {
        this.emptyView = emptyView;
        checkIfEmpty();
    }
}